<?php
class Webkul_Marketplacepartner_Model_Carrier_LocalDelivery extends Mage_Shipping_Model_Carrier_Abstract
{
    /* Use group alias */
    protected $_code = 'webkulshipping';
    public function collectRates(Mage_Shipping_Model_Rate_Request $request){
        // skip if not enabled
        if (!Mage::getStoreConfig('carriers/'.$this->_code.'/active'))
            return false;
        $result = Mage::getModel('shipping/rate_result');
        $handling = 0;
		$session = Mage::getSingleton('checkout/session');
		$mysqlprefix=Mage::getConfig()->getTablePrefix();
		$write = Mage::getSingleton('core/resource')->getConnection('core_write');
		foreach ($session->getQuote()->getAllItems() as $item) {
			$productcity = Mage::getModel('catalog/product');
			$loadpro= $productcity->load($item->getProductId());
			$shiptype=$loadpro->getwebkulshipingtype();
			$attribute=$loadpro->getResource()->getAttribute("webkulshipingtype");
			$attributeval=$loadpro->getwebkulamountper();
			if($attribute->usesSource()){$options = $attribute->getSource()->getAllOptions(false);}	
			if($shiptype==$options[0]['value']||$shiptype==''){	
				if(Mage::getStoreConfig('carriers/'.$this->_code.'/handling_temp')=='P'){
				   $handling=$handling+($item->getQty()*($item->getBaseCalculationPrice()*(Mage::getStoreConfig('carriers/'.$this->_code.'/handling')/100)));
				}
				else{$handling=$handling+($item->getQty()*Mage::getStoreConfig('carriers/'.$this->_code.'/handling'));}	
			}
			else{
				if($shiptype==$options[1]['value']){$handling=$handling+($attributeval*$item->getQty());}
				else{$handling=$handling+($item->getQty()*($item->getBaseCalculationPrice()*($attributeval/100)));}
			}
		}
		
        $method = Mage::getModel('shipping/rate_result_method');
        $method->setCarrier($this->_code);
        $method->setCarrierTitle(Mage::getStoreConfig('carriers/'.$this->_code.'/title'));
        /* Use method name */
        $method->setMethod('delivery');
		$method->setMethodTitle(Mage::getStoreConfig('carriers/'.$this->_code.'/methodtitle'));
        $method->setCost($handling);
        $method->setPrice($handling);
        $result->append($method);
        return $result;	
    }
}
 